import { H2oHotEvent, H2oUpdate} from "../../utils/bo"
import { H2oMethod } from "../../utils/constant"
import { DoUpdateMoment } from "../../utils/httpcall"
import { dao } from "../../utils/repository"
// pages/hot/hotEvent.ts
Component({
  behaviors: ['wx://component-export'],
  options: {styleIsolation: 'apply-shared'},
  /**
   * 页面的初始数据
   */
  data: {
    batchData:100,
    botMsg:'下拉刷新',
    show:false,
    hotList:Array<H2oHotEvent>(),
    hot:new H2oHotEvent()
  },
  lifetimes:{
    created:function() {
    },
    attached: function () { 
      dao.recommendHot(true, (res:H2oHotEvent[])=>{this.setData({hotList:res})})
    }
  },
  methods:{
    onDoView(e:any){
      const hot = dao.getHotItem(e.currentTarget.dataset.id)
      if(hot !== undefined){
        this.setData({show:true, hot:hot})
      }
    },
    onClose(_e:any){
      const {type} = _e
      const hup = new H2oUpdate()
      hup.views = 1
      hup.loves = 0
      hup.tag = H2oMethod.Moment
      hup.id = this.data.hot.id
      if(type === "confirm"){
        hup.loves = 1
      } 
      DoUpdateMoment(hup)
      this.setData({show:false})
    },
    onScroll(e:any){
      if(e.type === 'scrolltolower'){
        dao.recommendHot(false, (evList:H2oHotEvent[])=>{
          if(evList.length > 0){
            const hotList = this.data.hotList
            hotList.push(...evList)
            this.setData({hotList:hotList})
          }else{
            this.setData({botMsg:'我是有底线的'})
          }
        })
      }else{
        this.setData({botMsg:'下拉刷新'})
      }
    }
  }
})